package com.evil.common.core.enums;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 日期格式类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum DateTypeEnum {
    /**
     * 日期格式
     */
    TIME(0, "时分秒", DatePattern.NORM_TIME_PATTERN, false),
    DATE(1, "年-月-日", DatePattern.NORM_DATE_PATTERN, true),
    MONTH(2, "年-月", "yyyy-MM", true),
    YEAR(3, "年", "yyyy", true),
    DATETIME(4, "年-月-日 时:分:秒", DatePattern.NORM_DATETIME_PATTERN, false),
    DATERANGE(5, "年-月-日", DatePattern.NORM_DATE_PATTERN, false),
    MONTH_RANGE(6, "年-月", "yyyy-MM", false),
    DATETIMERANGE(7, "年-月-日 时:分:秒", DatePattern.NORM_DATETIME_PATTERN, false),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final String pattern;
    /**
     * 报表维度可使用
     */
    @Getter
    private final boolean reporting;

    public static Optional<DateTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static DateTypeEnum isExistById(Integer id) {
        return DateTypeEnum.findById(id).orElseThrow(DateTypeEnum::getBusinessException);
    }

    /**
     * 获取报表可用 日期格式
     *
     * @param id id
     * @return Optional
     */
    public static Optional<DateTypeEnum> findReportingEnumById(Integer id) {
        return DateTypeEnum.findById(id).filter(DateTypeEnum::isReporting);
    }

    public String dateFormat(String dateStr) throws ParseException {
        Date date = DateUtil.parseDate(dateStr);
        return DateUtil.format(date, this.pattern);
    }

    public String dateFormat(Object dateStr) throws ParseException {
        Date date;
        if (dateStr instanceof Date) {
            date = (Date) dateStr;
        } else {
            date = DateUtil.parseDate(dateStr.toString());
        }
        return DateUtil.format(date, this.pattern);
    }

    public String dateFormatIgnoreError(String dateStr) {
        try {
            return this.dateFormat(dateStr);
        } catch (ParseException ignored) {
        }
        return dateStr;
    }

    /**
     * 报表维度时间切割
     *
     * @param dateStr dateStr
     * @return List
     */
    public List<String> reportingDateSplit(String dateStr) {
        try {
            return ListUtil.toList(DateUtil.format(DateUtil.parse(dateStr), this.pattern).split("-"));
        } catch (Exception e) {
            return ListUtil.toList(dateStr);
        }
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "日期格式参数错误");
    }
}
